package com.chenqi.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenqi.myblog.mapper.BlogMapper;
import com.chenqi.myblog.mapper.TypeMapper;
import com.chenqi.myblog.model.Blog;
import com.chenqi.myblog.model.Type;
import com.chenqi.myblog.model.User;
import com.chenqi.myblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @program: myblog
 * @author: ChenQi
 * @create: 2021-02-2021/2/2-10-13
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Type> findAllType() {
        return typeMapper.selectList(null);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void save(Type type) {
        typeMapper.insert(type);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void remove(Integer id) {
        typeMapper.deleteById(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Type> findSomeType(Integer start,Integer rows) {
        return typeMapper.findSomeType(start,rows);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Integer findTypeCountByBlog(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id", id);
        return blogMapper.selectCount(queryWrapper);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Type findTypeById(Integer id) {
        return typeMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = RuntimeException.class)
    public void deleteType(Integer id) {
        typeMapper.deleteById(id);
    }
}
